package cn.ddiancan.xddcloud.psc.flow.event;

import java.util.function.Supplier;

import cn.ddiancan.xddcloud.psc.flow.engine.core.FlowCreator;
import cn.ddiancan.xddcloud.psc.flow.enums.EventType;
import cn.ddiancan.xddcloud.psc.flow.entity.XddHisInstance;
import cn.ddiancan.xddcloud.psc.flow.engine.listener.InstanceListener;
import cn.ddiancan.xddcloud.psc.flow.engine.model.NodeModel;
import org.springframework.context.ApplicationEventPublisher;

/**
 * Spring boot Event 异步实例监听处理器
 * @since 1.0
 */
public class EventInstanceListener implements InstanceListener {

    private final ApplicationEventPublisher eventPublisher;

    public EventInstanceListener(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    @Override
    public boolean notify(EventType eventType, Supplier<XddHisInstance> supplier, NodeModel nodeModel,
        FlowCreator flowCreator) {
        InstanceEvent instanceEvent = new InstanceEvent();
        instanceEvent.setEventType(eventType);
        instanceEvent.setFlwInstance(supplier.get());
        instanceEvent.setNodeModel(nodeModel);
        instanceEvent.setFlowCreator(flowCreator);
        eventPublisher.publishEvent(instanceEvent);
        return true;
    }
}
